package com.lhy.wcss.common.config;

import com.lhy.wcss.common.utls.SessionUtils;
import com.lhy.wcss.model.domain.WhitelistSetting;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Objects;

@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        WhitelistSetting user= SessionUtils.getCurUser();
        if(Objects.isNull(user)){
            request.setAttribute("msg","请先登录");
//            request.getRequestDispatcher("/").forward(request,response);
            response.sendRedirect("/");

            /*return false;*/
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
